package cn.kailangtu.dataspot.activify;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import cn.kailangtu.commonevents.data.DataBean;
import cn.kailangtu.commonevents.eventEntity.KeyboardSelectionActionEvent;
import cn.kailangtu.dataspot.data.consts.KeyboardAction;
import cn.kailangtu.dataspot.data.context.KeyboardSelectContext;
import cn.kailangtu.dataspot.databinding.FragmentKeyboardSelectionBinding;

public class KeyboardSelectionFragment extends KeyboardBaseFragment {

    private FragmentKeyboardSelectionBinding binding;


    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentKeyboardSelectionBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initListener();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }



    private void initListener(){
        binding.btnSelectLeft.setOnClickListener(keyboardSelectionListener);
        binding.btnSelectRight.setOnClickListener(keyboardSelectionListener);
        binding.btnSelectSave.setOnClickListener(keyboardSelectionListener);
        binding.btnSelectCancel.setOnClickListener(keyboardSelectionListener);
    }

    private View.OnClickListener keyboardSelectionListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView current = (TextView)view;
            KeyboardSelectContext.setSelectedView(current);
            KeyboardSelectContext.entry();
            if(KeyboardSelectContext.result != null) {
                binding.tvCurrentValue.setText(KeyboardSelectContext.result.getLabel());
            }
        }
    };


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(KeyboardSelectionActionEvent event){
        selectKeyboard(event.getActionValue(),event.getActionData());
    }

    private void selectKeyboard(int action, DataBean data )
    {
        switch(action) {
            case KeyboardAction.OPEN:
                Log.d("SHOOTLEE","SELECT KEY BOARD: OPEN");
                if(KeyboardSelectContext.result != null) {
                    binding.tvCurrentValue.setText(KeyboardSelectContext.result.getLabel());
                    Log.d("SHOOTLEE", "SELECT KEY BOARD: OPEN:" + binding.tvCurrentValue.getText());
                }
                else
                {
                    Log.d("SHOOTLEE","SELECT KEY BOARD: OPEN:0");
                    binding.tvCurrentValue.setText(KeyboardSelectContext.goFirst());
                    Log.d("SHOOTLEE","SELECT KEY BOARD: OPEN:0"+binding.tvCurrentValue.getText());
                }
                break;
            case KeyboardAction.ENTRY:
                Log.d("CONTROL","ENTRY click");
                KeyboardSelectContext.entry();
                if(KeyboardSelectContext.result != null) {
                    binding.tvCurrentValue.setText(KeyboardSelectContext.result.getLabel());
                }
                break;
            case KeyboardAction.LEFT:
                Log.d("CONTROL","LEFT click");
                if(KeyboardSelectContext.getSelectedView() == null)
                {
                    KeyboardSelectContext.setSelectedView(binding.tvCurrentValue);
                }
                else if(KeyboardSelectContext.getSelectedView().equals(binding.tvCurrentValue))
                {
                    KeyboardSelectContext.setSelectedView(binding.btnSelectSave);
                }
                else if(KeyboardSelectContext.getSelectedView().equals(binding.btnSelectSave))
                {
                    KeyboardSelectContext.setSelectedView(binding.btnSelectCancel);
                }
                else if(KeyboardSelectContext.getSelectedView().equals(binding.btnSelectCancel))
                {
                    KeyboardSelectContext.setSelectedView(binding.tvCurrentValue);
                }
                else
                {
                    KeyboardSelectContext.setSelectedView(binding.tvCurrentValue);
                }
                break;
            case KeyboardAction.RIGHT:
                Log.d("CONTROL","RIGHT click");
                if(KeyboardSelectContext.getSelectedView() == null)
                {
                    KeyboardSelectContext.setSelectedView(binding.tvCurrentValue);
                }

                else if(KeyboardSelectContext.getSelectedView().equals(binding.tvCurrentValue))
                {
                    KeyboardSelectContext.setSelectedView(binding.btnSelectCancel);
                }
                else if(KeyboardSelectContext.getSelectedView().equals(binding.btnSelectCancel))
                {
                    KeyboardSelectContext.setSelectedView(binding.btnSelectSave);
                }
                else if(KeyboardSelectContext.getSelectedView().equals(binding.btnSelectSave)){
                    KeyboardSelectContext.setSelectedView(binding.tvCurrentValue);
                }
                else
                {
                    KeyboardSelectContext.setSelectedView(binding.tvCurrentValue);
                }
                break;
        }
    }
}
